package com.awakening.servlet;

import com.awakening.pojo.User;
import com.awakening.service.UserService;
import com.awakening.service.impl.UserServiceImpl;
import com.awakening.utils.UUIDUtils;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.sql.SQLException;

/**
 * 用户注册的Servlet
 * @Author:AwakeningCode
 * @Date: Created in 17:58 2018\1\22 0022
 */
public class RegistServlet extends HttpServlet{

    @Override
    protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        try {
            //处理中文乱码
            req.setCharacterEncoding("UTF-8");
            //接收数据
            String username = req.getParameter("username");
            String password = req.getParameter("password");
            String nickname = req.getParameter("nickname");
            String email = req.getParameter("email");
            //封装数据
            User user = new User();
            user.setUsername(username);
            user.setPassword(password);
            user.setNickname(nickname);
            user.setEmail(email);
            user.setState(0);//0:未激活 1：已经激活
            String code = UUIDUtils.getUUID() + UUIDUtils.getUUID();
            user.setCode(code);
            //调用业务层处理数据
            UserService userService = new UserServiceImpl();
            userService.regist(user);
            //页面跳转
            req.setAttribute("msg","您已经注册成功，请至邮箱激活！");
            req.getRequestDispatcher("/msg.jsp").forward(req,resp);
        }catch (Exception e){
            e.printStackTrace();
            throw new RuntimeException();
        }

    }

    @Override
    protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        doGet(req,resp);
    }
}
